package Framework;
/*
 * 
 * anc_Button
 * 
 * Notes
 *  -----------------------------------------------------------------------------------------
 *  The background image code is based on JImageButton, author unknown (It's all over google)
 *  http://code.google.com/p/willys-jukebox/source/browse/trunk/src/touch/src/view/JImageButton.java?spec=svn176&r=176
 *  
 *  To Do
 * 	-----------------------------------------------------------------------------------------
 * 
 *  Needs mouse down state implemented, and possibly mouseHover removed?
 *  
 * 	
 *  Revision History
 *  ------------------------------------------------------------------------------------------
 *  Who					Date			What's changed & Why & Notes
 * 	Colin Smith			1/10/2011		Created (Over the past week). 
 * 										I wouldn't modify any of this code. It's dirty as hell, I'm just 
 * 										submitting it so you can all see what stuff looks like.
 * 	Colin Smith			9/10/2011		Now defaults to an img/btn1.png if no image is set up. and works as expected.
 * 										Still needs a cleanup, but the code is far more readable
 * 
 */
import java.awt.FontMetrics;

import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.util.Vector;

import javax.swing.*;
 
public class btn_Ancestor extends JButton
{
	static String CLASS = "anc_Button";
	FontMetrics fontMetrics = this.getFontMetrics(ConfigOpts.BUTTON_FONT);
	ImageIcon img_Background_Standard; 
	ImageIcon img_Background_Hover; 
	ImageIcon img_Background_Down;
	
	public void setimg_Background_Standard( String as_image ) {
		img_Background_Standard = nv_ImageLoader.loadImage(as_image);
	}
	public void setimg_Background_Hover( String as_image ) {
		img_Background_Standard = nv_ImageLoader.loadImage(as_image);
	}
	public void setimg_Background_Down( String as_image ) {
		img_Background_Down = nv_ImageLoader.loadImage(as_image);
	}
	 
	public void paintComponent( Graphics g ) {
		ImageIcon l_icon_RenderBg;
		if (!this.getModel().isPressed()){
			l_icon_RenderBg = img_Background_Standard;
		} else if ( this.getModel().isPressed()){
			l_icon_RenderBg = img_Background_Down;
		} else {
			l_icon_RenderBg = img_Background_Standard;
		}
		
		g.drawImage(l_icon_RenderBg.getImage(), 0, 0, this.getWidth(), this.getHeight(), this);
		
		if( !getText().equals( "" ) ) {
			// TODO this needs to factor in strings width and height!
			
			
			
					//this.getStringBounds(this.getText(), g.getFontMetrics()),
					Font f = this.getFont();
					FontMetrics getFontMetrics2 = this.getFontMetrics(f);
					String s = this.getText();
					
					FontRenderContext context  = getFontMetrics2.getFontRenderContext();
					
					
					Rectangle2D bounds = f.getStringBounds(s, context);
					int size; 
					int size2;
					//size2 = (int) (this.getBounds().height - bounds.getHeight()) / 2;
					size2 = this.getBounds().height / 2;
					size = (int) ((this.getBounds().width - bounds.getWidth()) / 2);
					g.drawString( super.getText(), size, size2);
					
					
					//getHeight() / 2;
		}
		
		if( getIcon() != null ) {
			Icon icon = getIcon();
			icon.paintIcon( this, g, 10, 10 );
			}
		}
		
		protected void finalize() throws Throwable {
			debugMessage.log("Object " + CLASS + " Destructed.", 11);
	}
		
	public btn_Ancestor(String string) {
		super(string);
		// Setting default bounds. else appears offscreen by default on null content manager. 
		this.setBounds(0,0,200,75); 
		this.setBorder(null);
		this.setFont(ConfigOpts.BUTTON_FONT);
		this.setForeground(ConfigOpts.BUTTON_COLOR);
		
		this.setHorizontalAlignment(LEFT); // Needs to be set for paintComponent to work .
		
		setimg_Background_Standard( "img/btn1.png" );
		setimg_Background_Hover( "img/btn3.png" );
		setimg_Background_Down( "img/btn2.png" );
		//= "img/btn1.png";  = "img/btn3.png"; = "img/btn2.png";
		//this.setVerticleAlignment(CENTER)
	}
	
	//public boolean equals(Object obj){
	//	return super.equals(obj);
	//}
}